package com.zhao.service.bussness;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.zhao.common.support.MessageTypeEnum;
import com.zhao.data.mapper.MessageMapper;
import com.zhao.data.model.Message;
import com.zhao.service.task.AsyncMessageSender;

/**
 * 恩恩
 * 
 * @author zhaoyongfei
 *
 */
@Service
public class MessageService {
	protected static final Logger logger = LoggerFactory.getLogger(MessageService.class);

	@Value("${spring.mail.username}")
	String fromUser;

	@Autowired
	AsyncMessageSender asyncMessageSender;

	@Autowired
	MessageMapper messageMapper;

	public void sendMessage(MessageTypeEnum messageType, String subject, String toUser, String text) {
		Message message = new Message();
		message.setMessageType(messageType.getCode());
		message.setCreateTime(new Date());
		message.setSubject(subject);
		message.setToUser(toUser);
		message.setText(text);
		switch (messageType) {
		case EMAIL:
			message.setFromUser(fromUser);
			message.setIsRead(true);
			asyncMessageSender.sendEmail(message);
			break;
		case TEXT_EMAIL:
			message.setFromUser(fromUser);
			message.setIsRead(true);
			break;

		case MOBILE:
			//TODO 暂无短信接口
			message.setFromUser(fromUser);
			message.setIsRead(true);
			break;
		case SITE:
			message.setFromUser("system");
			message.setIsRead(false);
			break;
		default:
			break;
		}
		messageMapper.insert(message);
	}
}
